# -*- coding: utf8-*-


from mekk.xmind import XMindDocument
import sys
reload(sys)
sys.setdefaultencoding('utf8')

# spec为字符串，需要挑选出的
# allArray为所有的需要并行选项
#spec = u"用户"
#allArray = [u"用户", u"管理员"]
spec = unicode(sys.argv[1],"utf-8")
allArray = sys.argv[2].decode("utf-8",'ignore').encode("utf-8").split('，')
print spec
print allArray[0],allArray[1]

remainArray = list(set(allArray) - set(spec))

def printNode(node):
    print "* ", node.get_title()
    print "   markers: ", list(node.get_markers())

def printTree(root):
    printNode(root)
    for node in root.get_subtopics():
        printTree(node)

def fillPriority(root):
    if len(list(root.get_subtopics())) == 0:
        return;
    for node in root.get_subtopics():
        if  len(list(node.get_markers())) == 0:
            for mark in root.get_markers():
                node.add_marker(mark)
        fillPriority(node)


xmind = XMindDocument.open("testcase.xmind")
sheet = xmind.get_first_sheet()
root = sheet.get_root_topic()
fillPriority(root)

def migrateMask(org, des):
    for mark in org.get_markers():
        des.add_marker(mark)


def addAllSub(org, des):
    # print org.get_title()   
    if len(list(org.get_subtopics())) > 0:
        for subnode in org.get_subtopics():

            if subnode.get_title() == spec:        # 要保留的
                print subnode.get_title()
                for subsubnode in subnode.get_subtopics():
                    subsubdes = des.add_subtopic(subsubnode.get_title())
                    migrateMask(subsubnode, subsubdes)
                    addAllSub(subsubnode, subsubdes)
            elif subnode.get_title() in remainArray:     # 要去除的
                continue
            else:
                subdes = des.add_subtopic(subnode.get_title())
                migrateMask(subnode, subdes)
                addAllSub(subnode, subdes)

# 做输出
OUTPUT = spec + u".xmind"
xmind2 = XMindDocument.create(u"user", root.get_title())
first_sheet = xmind2.get_first_sheet()
root_topic = first_sheet.get_root_topic()

addAllSub(root, root_topic)
xmind2.save(OUTPUT)

